<template>
  <div id="app">
    <el-tree
      :data="data"
      :props="defaultProps"
      @node-click="handleNodeClick"
      show-checkbox
      node-key="catId"
      :default-expanded-keys="expandedKeys"
      :default-expand-all='false'
      :expand-on-click-node="false"
    >
      <span
        class="custom-tree-node"
        slot-scope="{ node, data }"
      >
        <span>{{ node.label }}</span>
        <span>
          <el-button
            v-if="node.level <= 2"
            type="text"
            size="mini"
            @click="() => append(data)"
          >
            <i class="el-icon-plus">追加</i>
          </el-button>
          <el-button
            v-if="node.childNodes.length == 0"
            type="text"
            size="mini"
            @click="() => remove(node, data)"
          >
            <i class="el-icon-delete">删除</i>
          </el-button>
          <el-button
            type="text"
            size="mini"
            @click="()=>edit(data)"
          >
            <i class="el-icon-edit">修改</i>

          </el-button>
        </span>
      </span>
    </el-tree>
    <el-dialog
      :title="title"
      :dialogType='dialogType'
      :visible.sync="dialogFormVisible"
    >
      <el-form :model="category">
        <el-form-item label="分类名称">
          <el-input
            v-model="category.name"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="分类图标">
          <el-input
            v-model="category.icon"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="计量单位">
          <el-input
            v-model="category.productUnit"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button
          type="primary"
          @click="submitForm()"
        >确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data () {
    return {
      data: [],
      defaultProps: {
        children: 'childCategory',
        label: 'name'
      },
      expandedKeys: [],
      category: {
        name: '',
        parengCid: '',
        catLevel: '',
        showStatus: 1,
        sort: 0,
        icon: '',
        productUnit: ''
      },
      dialogFormVisible: false,
      title: '',
      dialogType: ''
    }
  },
  methods: {
    handleNodeClick (data) {
      console.log(data)
    },
    getCategoruListTree () {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get'
      }).then(({data}) => {
        this.data = data.data
        console.log(this.data)
        this.dataListLoading = false
      })
    },
    submitForm () {
      if (this.dialogType === 'add') {
        this.addCategory()
      } else {
        this.editCategory()
      }
    },
    append (data) {
      this.title = '新增分类'
      this.dialogType = 'add'
      this.dialogFormVisible = true
      // 新增分类的父id为当前分类的parengCid
      this.category.parentCid = data.catId
      // 新增分类的层级为当前分类的层级加一
      this.category.catLevel = data.catLevel * 1 + 1
    },
    edit (data) {
      this.title = '修改分类'
      this.dialogType = 'edit'
      this.dialogFormVisible = true
      // 查询数据 实现数据回显
      this.$http({
        url: this.$http.adornUrl(`/product/category/info/${data.catId}`),
        method: 'get'
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.category = data.category
        } else {
          this.category = null
        }
      })
    },
    addCategory () {
      console.log(this.category)
      this.$http({
        url: this.$http.adornUrl('/product/category/save'),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '新增成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.dialogFormVisible = false
              this.category.name = ''
              this.getCategoruListTree()
              this.expandedKeys = [this.category.parentCid]
            }
          })
        } else {
          this.$message.error(data.message)
        }
      }).catch(() => {})
    },
    editCategory () {
      this.$http({
        url: this.$http.adornUrl('/product/category/update'),
        method: 'post',
        data: this.$http.adornData(this.category, false)
      }).then(({data}) => {
        if (data && data.code === 0) {
          this.$message({
            message: '修改成功',
            type: 'success',
            duration: 1500,
            onClose: () => {
              this.dialogFormVisible = false
              this.category.name = ''
              this.category.icon = ''
              this.category.productUnit = ''
              this.getCategoruListTree()
              this.expandedKeys = [this.category.parentCid]
            }
          })
        } else {
          this.$message.error(data.message)
        }
      })
    },
    remove (node, data) {
      var ids = [data.catId]
      this.$confirm(`确定要删除【${data.name}】分类`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/product/category/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then(({data}) => {
          if (data && data.code === 0) {
            this.$message({
              message: '删除成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getCategoruListTree()
                // 设置默认展开节点
                this.expandedKeys = [node.parent.data.catId]
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      }).catch(() => {})
    }
  },
  created () {
    this.getCategoruListTree()
  }
}
</script>

<style>
</style>